package net.zjitc.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author admin
 * @Date 2019/4/24 9:04
 * 更新操作
 */
public class Test2 {
    public static void main(String[] args) {
        /* 1、注册驱动
        *  2、获得数据库连接
        *  3、获得能操作SQL语句的对象
        *  4、执行SQL语句*/
        Connection conn=null;
        Statement stmt=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/pyt";
            String user="root";
            String password="123456";
            conn= DriverManager.getConnection(url,user,password);
            stmt=conn.createStatement();
            String sql="UPDATE USER SET age=25 WHERE NAME='zhangsan'";
            int c=stmt.executeUpdate(sql);
            System.out.println("影响了"+c+"行");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
